<template>
	<section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.name" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="loadListData">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!--列表-->
    <el-table :data="listDatas" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="姓名" width="120" sortable>
      </el-table-column>
      <el-table-column prop="sex" label="性别" width="100" :formatter="formatSex" sortable>
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="100" sortable>
      </el-table-column>
      <el-table-column prop="email" label="生日" width="120" sortable>
      </el-table-column>
      <el-table-column prop="phone" label="地址" min-width="180" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="5" :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--新增界面-->
    <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form :model="employee" label-width="80px" :rules="addFormRules" ref="empAddForm">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="employee.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="employee.phone" ></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
	</section>
</template>

<script>
import {addUser, removeUser} from "../../api/api";
import util from "../../common/js/util";

  export default {
		data() {
			return {
        listDatas:[],//用于列表展示
        listLoading:false,
        sels:[],
        total:0,
        currentPage:1,
        filters:{
          name:''
        },
        addFormVisible:false,
        employee:{
          id:'',
          username:'',
          phone:''
        },
        addFormRules: {//定义验证规则
          username: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ]
        },
        addLoading:false

			}
		},
		methods: {
      //显示编辑界面
      handleEdit(index, row) {
        this.addFormVisible = true;
        this.employee = Object.assign({}, row);
      },
      addSubmit(){
        this.$refs.empAddForm.validate((valid) => {
          if (valid) {
            this.$http.put("/api/employee",this.employee).then(res=>{
              let {success,message}=res.data
              if (success){
                this.$message({
                  showClose: true,
                  message: '操作成功',
                  type: 'success'
                });
              }else {
                this.$message.error('网络异常');
              }
              this.loadListData();
              this.addFormVisible=false
            })
          }
        });
      },
      handleAdd(){
        //1.清空数据
        this.employee={
          id:'',
          username:'',
          phone:''
        }
        //2.打开模态框
        this.addFormVisible=true;
      },
      handleDel(index,row){
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.$http.delete("/api/employee/"+row.id).then(res=>{
            let {success,message}=res.data
            if (success){
              this.$message({
                showClose: true,
                message: '删除成功',
                type: 'success'
              });
            }else {
              this.$message.error('网络异常');
            }
            this.loadListData();

          })
        }).catch(() => {
          this.$message('你已取消删除');
        });
      },
      handleCurrentChange(val) {
        this.currentPage = val;
        this.loadListData();
      },
      batchRemove(){

      },
      selsChange(val){
        this.sels=val;
      },
      formatSex(i,row){
        return "未知"
      },
      loadListData(){

        let param={
          currentPage:this.currentPage,
          pageSize:5,
          keyWords:this.filters.name
        }
        //请求后台数据
        this.$http.post("/api/employee",param).then(res=>{
          this.listDatas=res.data.data;
          this.total=res.data.total;
        })

      }


		},
		mounted() {
      this.loadListData()
		}
	}

</script>

<style scoped>

</style>